package com.qf.qfleadnewscommons.sms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(SmsProperties.class)
@ConditionalOnProperty(value = "sms.enable",havingValue = "true", matchIfMissing = false)
public class SmsAutoConfiguration {

    @Autowired
    private SmsProperties properties;

    @Bean
    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(properties.getAccessKeyId())
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(properties.getAccessKeySecret());
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    @Bean
    public SmsOperator smsOperator(com.aliyun.dysmsapi20170525.Client createClient,SmsProperties properties){
        return new SmsOperator(createClient,properties);
    }


}
